package com.newlink.building.userinfo.activity

import android.app.AlertDialog
import android.app.admin.DeviceAdminReceiver
import android.app.admin.DevicePolicyManager
import android.content.ComponentName
import android.content.Intent
import android.provider.Settings
import android.view.View
import androidx.core.net.toUri
import com.newlink.building.common_base.base.NL_BaseActivity
import com.newlink.building.common_base.base.BaseApplication.Companion.context
import com.newlink.building.common_base.ext.showToast
import com.newlink.building.common_base.widget.view.SettingsItemView
import com.newlink.building.userinfo.databinding.ActivityPermissionBinding
import com.newlink.building.userinfo.utils.Module_UserInfo_PermitUtils


/**
 * @Author: Jake
 * @Date: 2024-03-13
 * @Description:
 */
class Module_UserInfo_PermissionActivityNL : NL_BaseActivity() {

    //    override fun attachLayoutRes(): Int = R.layout.activity_permission
    private lateinit var mBinding: ActivityPermissionBinding
    override fun attachLayoutRes(): View {
        mBinding = ActivityPermissionBinding.inflate(layoutInflater)
        return mBinding.root
    }

    override fun initData() {
//        val titlebar = mBinding.titleBar
//        mBinding.titleBar.root.tvTitleBarCenter.text = "权限申请"
    }

    override fun initView() {
        mBinding.permissionOverApps.setOnSettingsItemClickListener(object :
            SettingsItemView.OnItemClickLister {
            override fun onSettingsItemClick(v: View) {
                requestOverAppsShow()
            }
        })

        mBinding.permissionRunInBackground.setOnSettingsItemClickListener(object :
            SettingsItemView.OnItemClickLister {
            override fun onSettingsItemClick(v: View) {
                Module_UserInfo_PermitUtils.startAppSettingActivity(context)
            }
        })

        mBinding.permissionBatteryOptimization.setOnSettingsItemClickListener(object :
            SettingsItemView.OnItemClickLister {
            override fun onSettingsItemClick(v: View) {
                requestBackendRunPermissions()
            }
        })

        mBinding.permissionShowLockscreen.setOnSettingsItemClickListener(object :
            SettingsItemView.OnItemClickLister {
            override fun onSettingsItemClick(v: View) {
                requestLockScreenShow2()
            }
        })
    }

    private fun requestBackendRunPermissions() {
//        val packageName: String = context.packageName
//        if (!context.powerManager.isIgnoringBatteryOptimizations(packageName)) {
//            try {
//                val intent = Intent().apply {
//                    action = Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS
//                    flags = Intent.FLAG_ACTIVITY_NEW_TASK
//                    data = "package:$packageName".toUri()
//                }
//                startActivity(intent)
//            } catch (e: ActivityNotFoundException) {
//                showToast("请打开电池优化")
//            }
//        } else {
//            showToast("你已经忽略了电池优化")
//        }
    }

    private fun requestOverAppsShow() {
        if (!Settings.canDrawOverlays(context)) {
            val builder = AlertDialog.Builder(context)
            builder.setTitle("请开启悬浮窗权限")
                .setMessage("请开启悬浮窗权限")
                .setPositiveButton("去开启") { dialog, which ->
                    val intent = Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION).apply {
                        data = "package:$packageName".toUri()
                    }
                    startActivity(intent)
                }
                .setNegativeButton("取消", null)
            builder.show()
        } else {
            showToast("你已经打开了悬浮窗权限")
        }
    }

    private fun requestLockScreenShow2() {
        val dpm = getSystemService(DEVICE_POLICY_SERVICE) as DevicePolicyManager
        if (dpm.isAdminActive(ComponentName(this, DeviceAdminReceiver::class.java))) {
            // 具有锁屏显示权限
        } else {
            // 没有锁屏显示权限
            Module_UserInfo_PermitUtils.startAppSettingActivity(context)
        }
    }
}